package com.zcwin.runner.model;

import java.util.Vector;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zcwin.runner.database.DAOFactory;
import com.zcwin.runner.database.IDAO;
import com.zcwin.runner.sms.bean.SendSmsBean;
import com.zcwin.runner.sms.bean.SmsServerConfig;
import com.zcwin.runner.utils.GTime;
import com.zcwin.runner.utils.GTimeExt;

/**
 *
 * 2015-9-9
 *
 * <li>Project: ThreadRunner
 * <li>Packgae:com.zcwin.runner.sms.model
 * <li>File Name: SendSmsUtil.java
 * <li>Description:
 * <li>Copyright: Copyright (c) 2009-2012 xinchuang Co. Ltd.
 * <li>Company: 河北信创信息技术有限公司
 *
 * @author 王士江
 * @version 1.0
 * $Id: SendSmsUtil.java,v 1.1 2017/10/21 13:05:39 wsjjiang Exp $
 */
public class SendSmsUtil {
    IDAO dao = DAOFactory.getDao("0");
    Logger logger = LoggerFactory.getLogger(SendSmsUtil.class);
    public Vector querySendData(String tTime)
    {
    	String sSql = "SELECT ID,SUBJ,FILENO,MFROM,MAILMJNAME,PRIONAME,SENDTIME,RECVMOBILE,SMSSTATE,PTIME,PCOUNT,RETRY,SMSID FROM SMS_LOG  "
    			+ " WHERE (SMSSTATE = '0' OR SMSSTATE IS NULL) AND (PCOUNT IS NULL OR PCOUNT <= RETRY) "
    			+ "AND (PTIME IS NULL OR PTIME<'"+tTime+"') ORDER BY PCOUNT ASC,PRIO DESC,PTIME ASC";
		Vector ve= dao.selectEx(sSql, 10);
	    if(ve!=null && ve.size()>0)
	    {
	        return ve;
	    }
	    return null;
    }
    
    public String sendSms(SmsServerConfig config,SendSmsBean sb)
    {
    	String result = "-1";
    	Object[] objects;
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		// 创建wsdl访问客户端
		Client client = dcf.createClient(config.getUrl());		
		try {
			//测试Message方法
			String msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root><MsgList><MsgInfo>"
					 +"<MsgId>"+sb.getSmsid()+"</MsgId>"
			         +"<Content>"+sb.getSMSInfo()+"</Content>"
			         +"<PhoneNO>"+sb.getDestAddr()+"</PhoneNO>"
			         +"</MsgInfo></MsgList></Root> ";
			objects = client.invoke("Message", msg);
			result = objects[0].toString();
    	
		} catch (Exception e) {
			logger.error("NEWHERO短信发送错误：邮件ID="+sb.getRid()+",SMSID="+sb.getSmsid());
			e.printStackTrace();
		} 
		return result;
    }
    
    public void setSendSucess(SendSmsBean sb)
    {
    	String rid = sb.getRid();
    	String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
    	String tTime = GTimeExt.timeAccount(sTime, sb.getSmscallback(), 5, "yyyyMMddHHmmss");
		dao.update("UPDATE SMS_LOG SET PTIME='"+tTime+"',SMSID='"+sb.getSmsid()+"',SMSSTATE='1',PCOUNT="+(sb.getClcount()+1)+",RETRY="+sb.getSmsclcount()+" WHERE ID='" + rid + "'");
    }
    
    public void setSendError(SendSmsBean sb)
    {
        String rid = sb.getRid();
        String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
        //出错后，间隔五分钟再提醒第二次，预留足够时间去处理错误。
        String tTime = GTimeExt.timeAccount(sTime, sb.getTxinterval(), 5, "yyyyMMddHHmmss");
        if(sb.getClcount()<sb.getRetry())
        {
        	dao.update("UPDATE SMS_LOG SET PTIME='"+tTime+"',PCOUNT="+(sb.getClcount()+1)+",RETRY="+sb.getRetry()+" WHERE ID='" + rid + "'");
        }
        else
        {
        	dao.update("UPDATE SMS_LOG SET PTIME='"+sTime+"',SMSID='"+sb.getSmsid()+"',SMSSTATE='2',PCOUNT="+(sb.getClcount()+1)+",RETRY="+sb.getRetry()+" WHERE ID='" + rid + "'");
        }
    }
    
    public void setSendError(String rid,String state)
    {
        String sTime = GTime.getTime(GTime.YYYYMMDDhhmmss);
        dao.update("UPDATE SMS_LOG SET PTIME='"+sTime+"',SMSSTATE='"+state+"',PCOUNT=1 WHERE ID='" + rid + "'");
    }
    
    public static void main(String[] args) {
        System.setProperty("SEEN.RUNNER.HOME", "D:/workspace3.7/ThreadRunner_TS");
        System.setProperty("user.timezone","GMT+08");
        PropertyConfigurator.configure(System.getProperty("SEEN.RUNNER.HOME") + "/config/log4j.properties"); 
        //SendSmsUtil su = new SendSmsUtil();
    }
}
